package problem39;

//39.组合总和
//https://leetcode.cn/problems/combination-sum/

import java.util.ArrayList;
import java.util.List;

class Solution {
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int sum = 0;
    int target;
    int[] nums;
    public List<List<Integer>> combinationSum(int[] candidates, int _target) {
        nums = candidates;
        target = _target;
        dfs(0);
        return ret;
    }
    public void dfs(int u) {
        if(sum > target) return;
        if(sum == target) {
            ret.add(new ArrayList(path));
            return;
        }
        
        for(int i = u; i<nums.length; i++) {
            sum += nums[i];
            path.add(nums[i]);
            dfs(i);
            sum -= nums[i];
            path.remove(path.size()-1);
        }
    }
}